package com.nantian.nfcm.bms.report.service;

import com.nantian.nfcm.bms.loan.dao.LoanDetailedReportDao;
import com.nantian.nfcm.bms.loan.entity.LoanDetailedReport;
import com.nantian.nfcm.bms.report.vo.LoanDetailedReportBean;
import com.nantian.nfcm.util.ServiceException;
import com.nantian.nfcm.util.vo.GridData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

@Service
public class LoanDetailedReportService {
    private LoanDetailedReportDao loanDetailedReportDao;

    @Autowired
    public LoanDetailedReportService(LoanDetailedReportDao loanDetailedReportDao) {
        this.loanDetailedReportDao = loanDetailedReportDao;
    }

    public GridData<LoanDetailedReport> findByCondition(int page, int size, LoanDetailedReportBean loanDetailedReportBean)
            throws ServiceException {
        try{
            Pageable pageable = new PageRequest(page, size, Sort.Direction.ASC,"loanId");
        	//Pageable pageable = new PageRequest(page, size);
            Specification<LoanDetailedReport> specification = (root, query, criteriaBuilder) -> {
                List<Predicate> predicates = new ArrayList<>();
                if (loanDetailedReportBean.getBusinessType() != null && !loanDetailedReportBean.getBusinessType().equals("")) {
                    Predicate businessType = criteriaBuilder.equal(root.get("businessType").as(String.class), loanDetailedReportBean.getBusinessType());
                    predicates.add(businessType);
                }

                if (loanDetailedReportBean.getLoanResult() != null && !loanDetailedReportBean.getLoanResult().equals("")) {
                    Predicate loanResult = criteriaBuilder.equal(root.get("loanResult").as(String.class),loanDetailedReportBean.getLoanResult());
                    predicates.add(loanResult);
                }

                if (loanDetailedReportBean.getProcessStat() != null && !loanDetailedReportBean.getProcessStat().equals("")) {
                    Predicate processStat = criteriaBuilder.equal(root.get("processStat").as(String.class),loanDetailedReportBean.getProcessStat());
                    predicates.add(processStat);
                }

                String queryStartTime = loanDetailedReportBean.getQueryStartTime();
                String queryEndTime = loanDetailedReportBean.getQueryEndTime();
                if (queryStartTime != null && !queryStartTime.equals("")
                        &&queryEndTime != null && !queryEndTime.equals("") ) {
                    Predicate queryTime = criteriaBuilder.between(root.get("pickupTime").as(String.class),queryStartTime,queryEndTime);
                    predicates.add(queryTime);
                }

                query.where(criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()])));
                return query.getRestriction();
            };
            Page<LoanDetailedReport> loanDetailedReportPage = loanDetailedReportDao.findAll(specification, pageable);
            GridData<LoanDetailedReport> gridData = new GridData<>();
            gridData.setData(loanDetailedReportPage.getContent());
            gridData.setNumber(loanDetailedReportPage.getTotalElements());
            gridData.setPage(loanDetailedReportPage.getNumber());
            gridData.setTotalPage(loanDetailedReportPage.getTotalPages());
            return gridData;
        }
        catch (Exception e){
            throw  new ServiceException(e.getMessage());
        }
    }

    public LoanDetailedReport findByLoanId(String loanId) throws ServiceException{
        List<LoanDetailedReport> reports = loanDetailedReportDao.findAllByLoanId(loanId);
        if(reports!=null&&reports.size()>0){
            if(reports.size()!=1){
                throw new ServiceException("报表流水出现脏数据");
            }
        }
        else {
            return null;
        }
        return reports.get(0);
    }
}
